layui.define(['jquery', 'ieTest', 'sessionFun', 'layer', 'configure', 'suggest', 'configureCom', 'keyUp'], function (exports) {
    var $ = layui.jquery;
    var sessionFun = layui.sessionFun;
    var configure = layui.configure;
    var suggest = layui.suggest;
    var ieTest = layui.ieTest;
    var configureCom = layui.configureCom;
    var keyUp = layui.keyUp;
    document.oncontextmenu = new Function('event.returnValue=false;'); //禁用右键
    var drawingData = sessionFun.get(configure.SESSION_DRAWING_DATA);
    var mxDraw = {
        mxtime: '',
        mxOcx: {},
        zuobiaoArr: [],
        deleteCircle: function () {
            layer.confirm('删除意见后不可恢复，是否确定删除？', {
                title: '提示', btn: ['确定', '取消'], btn1: function (index) {
                    suggest.deleteSuggest().then(function (res) {
                        mxDraw.updateAllComment();
                        layer.close(index);
                    });
                }, btn2: function (index, layero) {
                    mxDraw.updateAllComment();
                    layer.close(index);
                }
            });
        },
        init: function (id, dwgfile, cabpath, boxId) {
            var s;
            var classid = '74A777F8-7A8F-4e7c-AF47-7074828086E2';
            var Sys = {};
            var ua = navigator.userAgent.toLowerCase();
            var drawHtml;
            if (ieTest.check() > 0) {
                //ie下
                drawHtml =
                    '<object class="drawingObj" classid="clsid:' + classid + '" id=\'' + id + '\' codebase=\'' + cabpath + '\' width=\'85%\' height=\'85%\' align=\'left\'> \n' +
                    '                           <param name=\'_Version\' value=\'65536\'>\n' +
                    '                           <param name=\'_ExtentX\' value=\'27067\'>\n' +
                    '                           <param name=\'_ExtentY\' value=\'10292\'>\n' +
                    '                           <param name=\'_StockProps\' value=\'0\'>\n' +
                    '                           <param name=\'DwgFilePath\' value=\'' + dwgfile + '\'>\n' +
                    '                           <param name=\'IsRuningAtIE\' value=\'1\'>\n' +
                    '                           <param name=\'EnablePrintCmd\' value=\'0\'>\n' +
                    '                           <param name=\'ShowCommandWindow\' value=\'1\'>\n' +
                    '                           <param name=\'ShowToolBars\' value=\'1\'>\n' +
                    '                           <param name=\'ShowModelBar\' value=\'1\'>\n' +
                    '                           <param name=\'Iniset\' value=\'UsingPathDrawSolid=Y\'>\n' +
                    '                           <param name=\'ToolBarFiles\' value=\'\'>\n' +
                    '                           <param name=\'ShowMenuBar\' value=\'0\'>\n' +
                    '                           <param name=\'EnableUndo\' value=\'1\'>\n' +
                    '                           <param name=\'ShowPropertyWindow\' value=\'0\'>\n' +
                    '                           <param name=\'EnableClipboard\' value=\'0\'>\n' +
                    '                           <param name=\'wmode\' value=\'opaque\'>\n' +
                    '                           <param name=\'UserName\' value=\'河北霄岭工程设计咨询有限公司\'>\n' +
                    '                           <param name=\'UserSoftwareName\' value=\'易审宝电子审图系统\'>\n' +
                    '                           <param name=\'UserPhone\' value=\'0311-85299922\'>\n' +
                    '                           <param name=\'UserData\' value=\'HHHH2932B346CB4FB33268F82C067BD80C0998015277215224C6E03D9E26B44C1DDE3A149E372593928DE9D16CDE578C1AA78297000029325E9C61B7B1CC6382E90C654476367B157DC5F29C637EA5C0ABDD875AC6A8641859605577D0E5DBAAB4BDD00A3504745D00002932F2FCFB6D4737D0840367E365C6518B6AB36B3F5E8F97F3C2E2F784EDD76E22A7642259B26A325C32A44F19363C19BC7C000029320E1C775537C23684FA6AED6027262405D47230453973DCA15A9D8190AB7ACE6B0FADA2728233C4C1CDF7B6E931266615000029321F65F234DC44D3CD9422C3B0DD359E461DF5ED796E727C150BC76B5C61357E5254989B7480C3C590CDF7B6E9312666150000293219A2B18559AC2D82E4B6E921160F76EA76917611C7E3B96752C2300545AAB3FF9A43C186C0C98730AEADBBB20DC6CA0F000029324E04BC3FD3A723657CDC9C62888C91DF7FB9C9A177255B9632C6339EBD2C16BCC0504FC31BB30BDE70560B5C15BC00AD00002932A1BDA66E3CB4288316732E736DC0EAF1CB7E7105573BD5B1556EEC7CEC8E5487846C55E2628B3C65A046261D3B0432EA000029322CB400329EAF07A9A7A1AA45A3F982B39C82353FF3E11E90F985F70F136A0C1D2C7E326C36589EC896523CEE7F0005F400002932E46B8D7EFEE2C37161EF8EB65B23A773B38AC09926301DFC3E21E48EF469306F35A3039C8828DDBF70560B5C15BC00AD0000060A1FB7E03F08E3EACD0000\'>\n' +
                    '                    </object>';
            } else {
                //非ie下
                drawHtml = '<object class="drawingObj" clsid=\'{' + classid + '}\' id=\'' + id + '\' codebase=\'' + cabpath + '\' type=\'application/mxdraw-activex\' width=\'85%\' height=\'85%\' align=\'left\'> \n' +
                    '                           <param name=\'_Version\' value=\'65536\'>\n' +
                    '                           <param name=\'_ExtentX\' value=\'27067\'>\n' +
                    '                           <param name=\'_ExtentY\' value=\'10292\'>\n' +
                    '                           <param name=\'_StockProps\' value=\'0\'>\n' +
                    '                           <param name=\'DwgFilePath\' value=\'' + dwgfile + '\'>\n' +
                    '                           <param name=\'IsRuningAtIE\' value=\'1\'>\n' +
                    '                           <param name=\'EnablePrintCmd\' value=\'0\'>\n' +
                    '                           <param name=\'ShowCommandWindow\' value=\'1\'>\n' +
                    '                           <param name=\'ShowToolBars\' value=\'1\'>\n' +
                    '                           <param name=\'ShowModelBar\' value=\'1\'>\n' +
                    '                           <param name=\'Iniset\' value=\'UsingPathDrawSolid=Y\'>\n' +
                    '                           <param name=\'ToolBarFiles\' value=\'\'>\n' +
                    '                           <param name=\'ShowMenuBar\' value=\'0\'>\n' +
                    '                           <param name=\'EnableUndo\' value=\'1\'>\n' +
                    '                           <param name=\'ShowPropertyWindow\' value=\'0\'>\n' +
                    '                           <param name=\'EnableClipboard\' value=\'0\'>\n' +
                    '                           <param name=\'wmode\' value=\'opaque\'>\n' +
                    '                           <param name=\'UserName\' value=\'河北霄岭工程设计咨询有限公司\'>\n' +
                    '                           <param name=\'UserSoftwareName\' value=\'易审宝电子审图系统\'>\n' +
                    '                           <param name=\'UserPhone\' value=\'0311-85299922\'>\n' +
                    '                           <param name=\'UserData\' value=\'HHHH2932B346CB4FB33268F82C067BD80C0998015277215224C6E03D9E26B44C1DDE3A149E372593928DE9D16CDE578C1AA78297000029325E9C61B7B1CC6382E90C654476367B157DC5F29C637EA5C0ABDD875AC6A8641859605577D0E5DBAAB4BDD00A3504745D00002932F2FCFB6D4737D0840367E365C6518B6AB36B3F5E8F97F3C2E2F784EDD76E22A7642259B26A325C32A44F19363C19BC7C000029320E1C775537C23684FA6AED6027262405D47230453973DCA15A9D8190AB7ACE6B0FADA2728233C4C1CDF7B6E931266615000029321F65F234DC44D3CD9422C3B0DD359E461DF5ED796E727C150BC76B5C61357E5254989B7480C3C590CDF7B6E9312666150000293219A2B18559AC2D82E4B6E921160F76EA76917611C7E3B96752C2300545AAB3FF9A43C186C0C98730AEADBBB20DC6CA0F000029324E04BC3FD3A723657CDC9C62888C91DF7FB9C9A177255B9632C6339EBD2C16BCC0504FC31BB30BDE70560B5C15BC00AD00002932A1BDA66E3CB4288316732E736DC0EAF1CB7E7105573BD5B1556EEC7CEC8E5487846C55E2628B3C65A046261D3B0432EA000029322CB400329EAF07A9A7A1AA45A3F982B39C82353FF3E11E90F985F70F136A0C1D2C7E326C36589EC896523CEE7F0005F400002932E46B8D7EFEE2C37161EF8EB65B23A773B38AC09926301DFC3E21E48EF469306F35A3039C8828DDBF70560B5C15BC00AD0000060A1FB7E03F08E3EACD0000\'>\n' +
                    '                    </object>';
            }
            $(boxId).html(drawHtml);

            mxDraw.mxOcx = $('#' + id)[0];
            mxDraw.mxtime = setTimeout(function () {
                mxDraw.initMxDrawX();
                document.onkeyup = function (e) {
                    keyUp.down(e).then(function (res) {
                        switch (res.result) {
                            case configure.KEY_CODE_DELETE:
                                mxDraw.deleteCircle();
                                break;
                        }
                    });
                };
            }, 500);

        },
        // 变换自定义实体
        TransformByFun: function (pCustomEntity, pMatXform) {
            var sGuid = pCustomEntity.Guid;
            if (sGuid == "TestMxCustomEntity") {
                if (!pCustomEntity.IsHave("ept"))
                    return;

                var stp = pCustomEntity.GetPoint("spt");
                if (stp == null)
                    return;

                var ept = pCustomEntity.GetPoint("ept");
                if (ept == null)
                    return;
                var tpt = pCustomEntity.GetPoint("tpt");
                if (tpt == null)
                    return;
                tpt.TransformBy(pMatXform);
                stp.TransformBy(pMatXform);
                ept.TransformBy(pMatXform);

                pCustomEntity.SetPoint("spt", stp);
                pCustomEntity.SetPoint("tpt", tpt);
                pCustomEntity.SetPoint("ept", ept);
                mxDraw.mxOcx.SetEventRet(1);
            }
        },
        // 返回自定义实体最小外包
        GetGeomExtentsFun: function (pCustomEntity) {
            var sGuid = pCustomEntity.Guid;
            if (sGuid == "TestMxCustomEntity") {
                if (!pCustomEntity.IsHave("ept"))
                    return;

                var stp = pCustomEntity.GetPoint("spt");
                if (stp == null)
                    return;

                var ept = pCustomEntity.GetPoint("ept");
                if (ept == null)
                    return;
                var tpt = pCustomEntity.GetPoint("tpt");
                if (tpt == null)
                    return;
                tpt.TransformBy(pMatXform);
                stp.TransformBy(pMatXform);
                ept.TransformBy(pMatXform);

                pCustomEntity.SetPoint("spt", stp);
                pCustomEntity.SetPoint("tpt", tpt);
                pCustomEntity.SetPoint("ept", ept);
                mxDraw.mxOcx.SetEventRet(1);
            }
        },
        //拖放动态绘制函数。
        DoDynWorldDrawFun: function (dX, dY, pWorldDraw, pData) {
            var sGuid = pData.Guid;
            mxDraw.mxOcx.SetEventRet(0);
            // 当前拖放位置点.
            var curPt = mxOcx.NewPoint();
            curPt.x = dX;
            curPt.y = dY;

            if (sGuid == "DrawComment") {
                DynWorldDrawComment(pData, pWorldDraw, curPt);
                mxDraw.mxOcx.SetEventRet(1);
            } else if (sGuid == "DrawComment2") {
                mxDraw.DynWorldDrawComment2(pData, pWorldDraw, curPt);
                mxDraw.mxOcx.SetEventRet(1);
            } else if (sGuid == "DrawCircle") {
                var pt1 = pData.GetPoint("pt1");
                var dR = curPt.DistanceTo(pt1) * 0.5;
                var vec = curPt.SumVector(pt1);
                vec.Mult(0.5);
                pt1.Add(vec);
                pWorldDraw.DrawCircle(pt1.x, pt1.y, dR);
            }
        },
        //交互绘制批注时的动态绘制1
        DynWorldDrawComment2: function (pCustomEntity, pWorldDraw, curPt) {
            // 得到绘制参数.
            var sText = pCustomEntity.GetString("Text");
            var dTextHeight = pCustomEntity.GetDouble("TextHeight");
            var iEdgeNumber = pCustomEntity.GetLong("EdgeNumber");
            var dShapRadius = pCustomEntity.GetDouble("ShapRadius");
            var basePoint = pCustomEntity.GetPoint("BasePoint");

            // 创建一个批注对象.
            var comment = mxDraw.mxOcx.NewEntity("IMxDrawComment");
            comment.Text = sText;
            comment.TextHeight = dTextHeight;
            comment.EdgeNumber = iEdgeNumber;
            comment.ShapRadius = dShapRadius;
            comment.basePoint = basePoint;
            comment.Position = curPt;

            // 设置文字样式。
            pWorldDraw.TextStyle = "MyCommentFont";

            // 动态绘制.
            pWorldDraw.DrawEntity(comment);
        },
        // 自定义实体绘制函数
        ExplodeFun: function (pCustomEntity, pWorldDraw, txt) {
            mxDraw.mxOcx.AddTextStyle1("MyTextStyle", "tssdeng.shx", "hztxt.shx", 1);

            var sGuid = pCustomEntity.Guid;
            if (sGuid == "TestMxCustomEntity") {
                if (!pCustomEntity.IsHave("ept"))
                    return;

                var stp = pCustomEntity.GetPoint("spt");
                if (stp == null)
                    return;

                var ept = pCustomEntity.GetPoint("ept");
                if (ept == null)
                    return;
                var tpt = pCustomEntity.GetPoint("tpt");
                if (tpt == null)
                    return;
                var txt = pCustomEntity.GetString("infoTxt");
                if (txt == null)
                    return;
                var articleType = pCustomEntity.GetString("articleType");
                if (articleType == 1020) {
                    articleType = "强条";
                } else {
                    articleType = "";
                }
                var draNo = pCustomEntity.GetString("draNo");
                var ztdx = pCustomEntity.GetString("ztdx");
                var Author = articleType + "  图号：" + draNo;
                if (Author == null)
                    return;
                var mxUtility = mxDraw.mxOcx.NewUtility();
                pWorldDraw.SetColorIndex(1);
                pWorldDraw.TextStyle = "MyTextStyle";
                pWorldDraw.LineWidth = 1;
                pWorldDraw.DrawLine(stp.x, stp.y, stp.x, ept.y);
                pWorldDraw.DrawLine(stp.x, stp.y, ept.x, stp.y);
                pWorldDraw.DrawLine(ept.x, ept.y, ept.x, stp.y);
                pWorldDraw.DrawLine(ept.x, ept.y, stp.x, ept.y);

                pWorldDraw.DrawLine(ept.x, ept.y, tpt.x, tpt.y);

                pWorldDraw.DrawText(tpt.x, tpt.y + 100, txt, parseInt(ztdx), 0,
                    0, 1);
                pWorldDraw.DrawText(tpt.x, tpt.y - parseInt(ztdx) - 80, Author, parseInt(ztdx), 0,
                    0, 1);

                var tmpText = mxDraw.mxOcx.NewEntity("IMxDrawText");
                tmpText.TextString = txt;
                tmpText.Height = parseInt(ztdx);

                var txtBox = tmpText.GetBoundingBox2();
                if (txtBox != null) {
                    var pt1 = txtBox.Item(0);
                    var pt2 = txtBox.Item(1);

                    var txtWidth = pt2.x - pt1.x;

                    pWorldDraw.DrawLine(tpt.x, tpt.y, tpt.x + txtWidth, tpt.y);
                }
                mxDraw.mxOcx.SetEventRet(1);

            }
        },
        // 返回自定义实体夹点
        GetGripPointsFun: function (pCustomEntity) {
            var sGuid = pCustomEntity.Guid;
            if (sGuid == "TestMxCustomEntity") {
                if (!pCustomEntity.IsHave("ept"))
                    return;

                var stp = pCustomEntity.GetPoint("spt");
                if (stp == null)
                    return;

                var ept = pCustomEntity.GetPoint("ept");
                if (ept == null)
                    return;

                var tpt = pCustomEntity.GetPoint("tpt");
                if (tpt == null)
                    return;

                var ret = mxDraw.mxOcx.NewResbuf();


                ret.AddPoint(stp);
                ret.AddPoint(ept);
                ret.AddPoint(tpt);

                var midPt = mxDraw.mxOcx.NewPoint();
                midPt.x = stp.x + (ept.x - stp.x) / 2.0;
                midPt.y = stp.y + (ept.y - stp.y) / 2.0;
                ret.AddPoint(midPt);
                mxDraw.mxOcx.SetEventRetEx(ret);
            }
        },
        // 移动自定义实体夹点
        MoveGripPointsFun: function (pCustomEntity, lGridIndex, dOffsetX, dOffsetY) {
            var sGuid = pCustomEntity.Guid;
            if (sGuid == "TestMxCustomEntity") {
                if (!pCustomEntity.IsHave("ept"))
                    return;

                var stp = pCustomEntity.GetPoint("spt");
                if (stp == null)
                    return;

                var ept = pCustomEntity.GetPoint("ept");
                if (ept == null)
                    return;

                var tpt = pCustomEntity.GetPoint("tpt");
                if (tpt == null)
                    return;

                var randomid = pCustomEntity.GetString("randomid");
                if (randomid == null)
                    return;

                if (lGridIndex == 1) {
                    ept.x = ept.x + dOffsetX;
                    ept.y = ept.y + dOffsetY;
                    pCustomEntity.SetPoint("ept", ept);
                } else if (lGridIndex == 3) {
                    // 中点移动.
                    ept.x = ept.x + dOffsetX;
                    ept.y = ept.y + dOffsetY;
                    pCustomEntity.SetPoint("ept", ept);

                    stp.x = stp.x + dOffsetX;
                    stp.y = stp.y + dOffsetY;
                    pCustomEntity.SetPoint("spt", stp);

                    tpt.x = tpt.x + dOffsetX;
                    tpt.y = tpt.y + dOffsetY;
                    pCustomEntity.SetPoint("tpt", tpt);
                }

                if (lGridIndex == 0) {
                    stp.x = stp.x + dOffsetX;
                    stp.y = stp.y + dOffsetY;
                    pCustomEntity.SetPoint("spt", stp);
                }
                if (lGridIndex == 2) {
                    tpt.x = tpt.x + dOffsetX;
                    tpt.y = tpt.y + dOffsetY;
                    pCustomEntity.SetPoint("tpt", tpt);
                }
                var zuobiao = stp.x + "," + stp.y + "," + stp.z;
                zuobiao += "," + ept.x + "," + ept.y + "," + ept.z;
                zuobiao += "," + tpt.x + "," + tpt.y + "," + tpt.z;
                mxDraw.mxOcx.SetEventRet(1);
                //将坐标信息信息放到一个数组中，原坐标和新坐标配对，定时器500毫秒轮询比较如果坐标有变化，则保存坐标到数据库。
                var ifExistZuobiao = false;
                for (var i = 0; i < zuobiaoArr.length; i++) {
                    if (zuobiaoArr[i].randomid == randomid) {
                        ifExistZuobiao = true;
                        zuobiaoArr[i].oldzuobiao = zuobiaoArr[i].newzuobiao;
                        zuobiaoArr[i].newzuobiao = zuobiao;
                    }
                }
                if (ifExistZuobiao == false) {
                    var zuobiaoTemp = {};
                    zuobiaoTemp.randomid = randomid;
                    zuobiaoTemp.oldzuobiao = "";
                    zuobiaoTemp.newzuobiao = zuobiao;
                    zuobiaoArr.push(zuobiaoTemp);
                }
            }
        },
        refreshZuobiao: function () {
            for (var i = 0; i < zuobiaoArr.length; i++) {
                if (zuobiaoArr[i] == "") {
                    continue;
                }
                var randomid = zuobiaoArr[i].randomid;
                var oldzuobiao = zuobiaoArr[i].oldzuobiao;
                var newzuobiao = zuobiaoArr[i].newzuobiao;
                if (oldzuobiao == "0" || oldzuobiao != newzuobiao) {
                    zuobiaoArr[i] = "";
                    jQuery.ajax({
                        type: 'post',
                        url: updateZuobiaoUrl,
                        data: {
                            "zuobiao": newzuobiao,
                            "randomid": randomid
                        },
                        dataType: "json",
                        error: function (value) {
                            ds.dialog.alert('移动失败');
                        },
                        success: function (value) {
                        }
                    });
                }
            }
        },
        initMxDrawX: function () {
            /**梦想的内置方法，需要自己实现**/
            mxDraw.mxOcx.ImplementCommandEventFun = mxDraw.DoCommandEventFunc;
            mxDraw.mxOcx.ImpExplodeFun = mxDraw.ExplodeFun;
            mxDraw.mxOcx.ImpGetGripPointsFun = mxDraw.GetGripPointsFun;
            mxDraw.mxOcx.ImpMoveGripPointsAtFun = mxDraw.MoveGripPointsFun;
            mxDraw.mxOcx.ImpTransformByFun = mxDraw.TransformByFun;
            mxDraw.mxOcx.ImpGetGeomExtentsFun = mxDraw.GetGeomExtentsFun;
            mxDraw.mxOcx.ImplementMouseEventFun = mxDraw.MouseEvent;
            mxDraw.mxOcx.ImpDynWorldDrawFun = mxDraw.DoDynWorldDrawFun;
            /**梦想的内置方法，需要自己实现**/
            if (mxDraw.mxOcx) {
                if (!mxDraw.mxOcx.IsIniting()) {
                    clearInterval(mxDraw.mxtime);
                    // mxDraw.mxOcx.SendStringToExecute('Mx_Properties');
                    /* var url = firstDWG_url;
                     var str = mxDraw.mxOcx.Download(url);
                     mxDraw.mxOcx.OpenDwgFile(str);*/
                    /*   if ($('#popDiv', window.parent.document)) {
                           $('#popDiv', window.parent.document).hide();
                       }*/
                    mxDraw.DoCmd(2);
                    mxDraw.GetAllEntity();
                }
            }
        },
        // 调取梦想意见圈阅方法
        DoCmd: function (iCmd) {
            mxDraw.mxOcx.DoCommand(iCmd);
        },
        //梦想意见圈阅方法回调
        DoCommandEventFunc: function (iCmd) {
            if (iCmd === 1) { // 新添加圈阅调取方法
                mxDraw.InsertCustomEntity();
            } else if (iCmd === 2) { // 将坐标同步到图纸上
                suggest.getList().then(function (res) {
                    $(res.result.records).each(function (index, item) {
                        if (item.coordinate) {
                            mxDraw.LoadCustomEntity(item);//显示图纸圈阅
                        }
                    });
                    mxDraw.mxOcx.ZoomPan(100, 100);
                    mxDraw.mxOcx.SetSysVarLong("ORTHOMODE", 0);
                    mxDraw.mxOcx.SetSysVarLong("OSMODE", 0);
                });
            }
        },
        //圈阅并提交意见
        InsertCustomEntity: function () {
            var getPt = mxDraw.mxOcx.NewComObject('IMxDrawUiPrPoint');
            getPt.message = '点取第一点';
            if (getPt.go() != 1)
                return;
            var frstPt = getPt.value();
            if (frstPt == null)
                return false;
            var utl = mxDraw.mxOcx.NewUtility();
            var secondPt1 = utl.GetCorner(frstPt, '点取范围第二点');
            if (secondPt1 == null)
                return;
            var getSecondPt = mxDraw.mxOcx.NewComObject('IMxDrawUiPrPoint');
            getSecondPt.message = '点取标注点';
            getSecondPt.basePoint = secondPt1;
            getSecondPt.setUseBasePt(true);
            if (getSecondPt.go() != 1)
                return;
            var secondPt = getSecondPt.value();
            if (secondPt == null)
                return;
            var coordinate = frstPt.x + ',' + frstPt.y + ',' + frstPt.z + ',' + secondPt1.x + ',' + secondPt1.y + ',' + secondPt1.z + ',' + secondPt.x + ',' + secondPt.y + ',' + secondPt.z;
            top.layer.open({
                type: 2,
                title: '圈阅意见',
                area: configureCom.Psize1,
                content: configure.EXPERT_ADD_SUGGEST_HTML,
                btnAlign: 'c',
                btn: ['保存', '取消'],
                yes: function (index, layero) {
                    var iframeWin = top.window.frames['layui-layer-iframe' + index]; //得到iframe页的窗口对
                    iframeWin.getData().then(function (res) {
                        var data = res.result;
                        data.coordinate = coordinate;
                        var ent = mxDraw.mxOcx.DrawCustomEntity('TestMxCustomEntity', '');
                        ent.SetPoint("spt", frstPt);
                        ent.SetPoint("ept", secondPt1);
                        ent.SetPoint("tpt", secondPt);
                        ent.SetString("MxObjectAppName", "quanyue");
                        ent.SetString("draNo", data.drawingNum);//图号
                        ent.SetString("articleType", data.articleType);//条纹性质
                        ent.SetString("qtIdvalue", data.qiangtiaoCode);//强条值
                        ent.SetString("infoTxt", data.adviseTxt);//意见内容
                        ent.SetString('ztdx', configure.circleFontSize); //字体大小
                        ent.SetString("adviseId", "");//意见id

                        ent.SetxDataString('draNo', 0, data.drawingNum);//图纸编号
                        ent.SetxDataString('articleType', 0, data.articleType);//条文性质
                        ent.SetxDataString('qiangtiaoCode', 0, data.qiangtiaoCode);//强条值
                        ent.SetxDataString('tiaowenNum', 0, data.tiaowenNum);
                        ent.SetxDataString('ztdx', 0, data.circleFontSize);//字体大小
                        ent.SetxDataString('infoTxt', 0, data.adviseTxt);//意见内容
                        ent.SetxDataString('adviseId', 0, data.adviseId);//意见id


                        suggest.add(data).then(function (res) {
                            top.layer.alert('添加成功！');
                            mxDraw.updateAllComment();//更新图纸视图


                        });
                        top.layer.close(index);

                    });
                }
            });
        },
        //显示图纸上的圈阅，不保存数据库
        // LoadCustomEntity: function (draNo, zuobiao, infoTxt, articleType, create_by, randomid, ztdx) {
        LoadCustomEntity: function (item) {
            var ent = mxDraw.mxOcx.DrawCustomEntity('TestMxCustomEntity', '');
            var frstPt = mxDraw.mxOcx.NewPoint();
            var secondPt = mxDraw.mxOcx.NewPoint();
            var threePt = mxDraw.mxOcx.NewPoint();
            var strtemp = item.coordinate.split(',');
            frstPt.x = strtemp[0];
            frstPt.y = strtemp[1];
            secondPt.x = strtemp[3];
            secondPt.y = strtemp[4];
            threePt.x = strtemp[6];
            threePt.y = strtemp[7];
            ent.SetPoint('spt', frstPt);
            ent.SetPoint('ept', secondPt);
            ent.SetPoint('tpt', threePt);
            ent.SetString('MxObjectAppName', 'quanyue');
            ent.SetString('draNo', item.drawingNum);
            ent.SetString('infoTxt', item.adviseTxt);
            ent.SetString('articleType', item.articleType);
            ent.SetString('ztdx', item.circleFontSize);
            ent.SetxDataString('draNo', 0, item.drawingNum);
            ent.SetxDataString('infoTxt', 0, item.adviseTxt);
            ent.SetxDataString('articleType', 0, item.articleType);
            ent.SetxDataString('qiangtiaoCode', 0, item.qiangtiaoCode);
            ent.SetxDataString('tiaowenNum', 0, item.tiaowenNum);
            ent.SetxDataString('ztdx', 0, item.circleFontSize);
            ent.SetxDataString('adviseId', 0, item.adviseId);
            /*ent.SetString('create_by', create_by);
            ent.SetString('randomid', randomid);
            ent.SetxDataString('randomid', 0, randomid);*/
        },
        // 更新dwg视图
        updateAllComment: function () {
            var ss = mxDraw.mxOcx.NewSelectionSet();
            var spFilte = mxDraw.mxOcx.NewResbuf();
            /*// 把批注对象，当成过滤条件.
            spFilte.AddStringEx('MxComment,MxDrawXCustomEntity', 5020);
            // 过滤出所有批注
            ss.Select2(5, null, null, null, spFilte);*/
            var bFind = false;
            for (var i = 0; i < ss.Count; i++) {
                //循环从过滤的批注集合中获取对象
                var ent = ss.Item(i);
                if (ent == null)
                    continue;
                if (ent.ObjectName == 'McDbComment' || ent.ObjectName == 'MxDrawXCustomEntity') { //判断是否为批注对象
                    //判断当前批注ID和要删除的批注ID是否一致，如果一致则删除该批注,删除该批注后循环停止
                    ent.Erase();
                }
            }
            mxDraw.mxOcx.ZoomPan(100, 100);
            mxDraw.DoCmd(2);
        },
        // dwg图纸坐标处理方法
        randomString: function (len) {
            len = len || 32;
            var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
            /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
            var maxPos = $chars.length;
            var pwd = '';
            for (i = 0; i < len; i++) {
                pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
            }
            return pwd;
        },
        // 将所有实体显示，针对dwg签章
        GetAllEntity: function () {
            var database = mxDraw.mxOcx.GetDatabase();
            //判断是否有签章如果没有不再遍历
            /* var nCount = GetStampCount(database);
            if(nCount <= 0){
                return;
            } */
            var blockTable = database.GetBlockTable();
            var blkRec = blockTable.GetAt('*Model_Space'); // 获取模型空间
            // 创建一个用于遍历当前图纸空间的遍历器
            var iter = blkRec.NewIterator();
            if (iter != null) {
                for (; !iter.Done(); iter.Step(true, false)) {
                    // 得到遍历器当前的实体
                    var ent = iter.GetEntity();
                    if (ent.Visible == false && ent.ObjectName == 'McDbBlockReference') {
                        // 获取写入到块对象的数据,确保此对象是我们需要处理的
                        var xData = ent.GetXData('blocktype');
                        if (xData == null || xData.Count == 0)
                            continue;
                        var str = xData.AtString(1);
                        if (str != 'xiaolinqianzhang')
                            continue;
                        ent.Visible = true;
                    }
                }
            }
            blkRec = blockTable.GetAt('*Paper_Space');
            iter = blkRec.NewIterator();
            if (iter != null) {
                for (; !iter.Done(); iter.Step(true, false)) {
                    // 得到遍历器当前的实体
                    var ent = iter.GetEntity();
                    if (ent.Visible == false && ent.ObjectName == 'McDbBlockReference') {
                        // 获取写入到块对象的数据,确保此对象是我们需要处理的
                        var xData = ent.GetXData('blocktype');
                        if (xData == null || xData.Count == 0)
                            continue;
                        var str = xData.AtString(1);
                        if (str != 'xiaolinqianzhang')
                            continue;
                        ent.Visible = true;
                    }
                }
            }
            // 注意需要显示释放递代器.,不然会引起错误
            iter = null;
            // 获取数量
            // this.CollectGarbage();
        },
        // 获取数量
        GetStampCount: function (database) {
            var dict = database.GetNamedObjectsDictionary();
            if (dict == null) {
                return 0;
            }
            var subDict = dict.GetAt('ASDK_DICT', false);
            if (subDict == null) {
                return 0;
            }
            var xRecord = subDict.GetAt('stampcount');
            if (xRecord == null) {
                return 0;
            }
            var xData = xRecord.GetXRecordData();
            if (xData == null) {
                return 0;
            }
            return xData.AtLong(0);
        },
        // 暂时未知？？？
        uuid: function () {
            var s = [];
            var hexDigits = '0123456789abcdef';
            for (var i = 0; i < 36; i++) {
                s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
            }
            s[14] = '4';  // bits 12-15 of the time_hi_and_version field to 0010
            s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);  // bits 6-7 of the clock_seq_hi_and_reserved to 01
            s[8] = s[13] = s[18] = s[23] = '-';

            return s.join('');
        },
        //鼠标事件
        MouseEvent: function (dX, dY, lType) {
            if (lType == 5) {
                var focusTime = setTimeout(function () {
                    clearTimeout(focusTime);
                    mxDraw.mxOcx.focus();
                    var axMxDrawX1 = mxDraw.mxOcx;
                    var ss = axMxDrawX1.Call('Mx_NewSelectionSet', '');
                    var pt = axMxDrawX1.Call('Mx_NewPoint', '');
                    var fil = axMxDrawX1.Call('Mx_NewResbuf', '');
                    pt.x = dX;
                    pt.y = dY;
                    ss.SelectAtPoint(pt, fil);
                    if (ss.Count > 0) {
                        //获取对象id
                        var ent = ss.Item(0);
                        if (ent.ObjectName == 'McDbComment' || ent.ObjectName == 'MxDrawXCustomEntity') {
                            //得到当前标注信息
                            var data = {
                                drawingNum: ent.GetxDataString2('draNo', 0),
                                adviseTxt: ent.GetxDataString2('infoTxt', 0),
                                articleType: ent.GetxDataString2('articleType', 0),
                                qiangtiaoCode: ent.GetxDataString2('qiangtiaoCode', 0),
                                tiaowenNum: ent.GetxDataString2('tiaowenNum', 0),
                                circleFontSize: ent.GetxDataString2('ztdx', 0),
                                adviseId: ent.GetxDataString2('adviseId', 0)
                            };
                            sessionFun.set(configure.SESSION_SUGGEST_DATA, data); //将当前标注信息存入session
                        }
                    }
                }, 300);
            }
            //鼠标双击圈阅框
            if (lType == 4) {
                var axMxDrawX1 = mxDraw.mxOcx;
                var ss = axMxDrawX1.Call('Mx_NewSelectionSet', '');
                var pt = axMxDrawX1.Call('Mx_NewPoint', '');
                var fil = axMxDrawX1.Call('Mx_NewResbuf', '');
                pt.x = dX;
                pt.y = dY;
                ss.SelectAtPoint(pt, fil);
                if (ss.Count > 0) {
                    //获取对象id
                    var ent = ss.Item(0);
                    if (ent.ObjectName == 'McDbComment' || ent.ObjectName == 'MxDrawXCustomEntity') {
                        //得到当前标注信息
                        var data = {
                            drawingNum: ent.GetxDataString2('draNo', 0),
                            adviseTxt: ent.GetxDataString2('infoTxt', 0),
                            articleType: ent.GetxDataString2('articleType', 0),
                            qiangtiaoCode: ent.GetxDataString2('qiangtiaoCode', 0),
                            tiaowenNum: ent.GetxDataString2('tiaowenNum', 0),
                            circleFontSize: ent.GetxDataString2('ztdx', 0),
                            adviseId: ent.GetxDataString2('adviseId', 0)
                        };
                        console.log(data);
                        sessionFun.set(configure.SESSION_SUGGEST_DATA, data); //将当前标注信息存入session
                        var title = configure.LAYER_TITLE_CHANGE_SUGGEST;
                        var canEdit = drawingData.canEdit;
                        if (canEdit) {
                            var url = configure.EXPERT_ADD_SUGGEST_HTML + '?type=change';
                            configureCom.klmyPop([url, title, 1], false, false, function (layero, index) {
                                window.saveForm = function (data) {
                                    suggest.change(data).then(function (res) {
                                        top.layer.msg(configure.MSG_SAVE);
                                        axMxDrawX1.SendStringToExecute('');
                                        mxDraw.updateAllComment();
                                    });
                                    top.layer.close(index);
                                }
                            });
                        } else {
                            var title = '意见查看';
                            var url = configure.EXPERT_SUGGEST_DETAIL_HTML;
                            configureCom.klmyPop([url, title]);
                        }

                    }
                }
                return 1;
            }
            return 0;
        },
        //意见列表上的删除意见操作
        //删除图纸上的圈阅意见，和对应的数据库数据
        DelComment: function (randomid) {
            var tempId = randomid;
            var ss = mxDraw.mxOcx.NewSelectionSet();
            var spFilte = mxDraw.mxOcx.NewResbuf();
            // 把批注对象，当着过滤条件.
            spFilte.AddStringEx('MxComment,MxDrawXCustomEntity', 5020);
            // 过滤出所有批注
            ss.Select2(5, null, null, null, spFilte);
            var bFind = false;
            for (var i = 0; i < ss.Count; i++) {
                //循环从过滤的批注集合中获取对象
                var ent = ss.Item(i);
                if (ent == null)
                    continue;
                if (ent.ObjectName == 'McDbComment' || ent.ObjectName == 'MxDrawXCustomEntity') { //判断是否为批注对象
                    //判断当前批注ID和要删除的批注ID是否一致，如果一致则删除该批注,删除该批注后循环停止
                    var val = ent.GetxDataString2('randomid', 0);
                    if (tempId == val) {
                        ent.Erase();
                        mxDraw.mxOcx.ZoomPan(100, 100);
                        $.ajax({
                            type: 'post',
                            url: deleteMessageUrl,
                            data: {
                                'randomid': val
                            },
                            dataType: 'json',
                            error: function (value) {
                                var errormsg = value.msg;
                                if (errormsg == '0')
                                    ds.dialog.alert('删除圈阅意见失败');
                            },
                            success: function (value) {
                                var success = value.msg;
                            }
                        });
                    }
                }
            }
            mxDraw.mxOcx.ZoomPan(100, 100);
        }
    };
    exports('mxDraw', mxDraw);
});
